<?php
namespace app\admin\controller;
use app\service\appletService;
use think\Controller;
use think\facade\Db;

class Applet extends Controller{
	public function getgood()
	{
		$data=Db::table("applet_goods")->select()->toArray();
		return json_encode($data);
	}
	//获取轮播图
	public function getimg()
	{
		//$data=Db::table("applet_carousel")->where("show_status","1")->column("pic");
		$data=Db::table("applet_carousel")->where("show_status","1")->select()->toArray();
		return json_encode($data);
	}
	//获取应用链接
	public function getapp()
	{
		$data=Db::table("applet_application")->where("ctype","1")->field('name,icon,url')->select()->toArray();;
		return json_encode($data);
	}
	public function getcentralmenu()
	{
		$data=Db::table("applet_application")->where("ctype","2")->field('name,icon,url')->limit(3)->select()->toArray();;
		return json_encode($data);
	}
	//小区列表
	public function  village()
	{
		if(isset($_GET['communityname'])){
			$communityname = $_GET['communityname'];
			$li = (new appletService())->communityName($communityname);
			foreach ($li as $l){
				$lis['adcode'] = $l['id'];
				$lis['address'] = $l['community_address'];
				$lis['city'] = $l['community_name'];
				$list[] = $lis;
			}
		}else{
			$li = (new appletService())->communityList();
			foreach ($li as $l){
				$lis['adcode'] = $l['id'];
				$lis['address'] = $l['community_address'];
				$lis['city'] = $l['community_name'];
				$list[] = $lis;
			}
		}
		if(empty($list)){
            return json_encode($li);
        }else{
            return json_encode($list);
        }
		 
	}
	
	//组团列表
	public function group()
    {
        $community_id = $_GET['adcode'];
        $list = (new appletService())->groupList($community_id);
		//var_dump($list);
        return json_encode($list);
    }
	
	//幢数列表
    public function building()
    {
		$community_id = $_GET['adcode'];
        $building = $_GET['group'];
        $list = (new appletService())->buildingList($building,$community_id);
		sort($list);
        return json_encode($list);
    }
    //单元列表
    public function unit()
    {
		$community_id = $_GET['adcode'];
        $building = $_GET['group'];
        $unit = $_GET['building'];
        $list = (new appletService())->unitList($unit,$building,$community_id);
         
		sort($list);
        return json_encode($list);
    }
    //房屋列表
    public function room()
    {
		$community_id = $_GET['adcode'];
        $building = $_GET['group'];
        $unit = $_GET['building'];
        $room = $_GET['unit'];
        $list = (new appletService())->roomList($room,$unit,$building,$community_id);
		sort($list);
        return json_encode($list);
    }
	
    public function test(){
    	$community_id="43";
    	$group='金成紫辰花园';
    	$building= '紫辰花园100幢';
    	$unit='1单元';
    	$room='101室';
    	$roominfo = (new appletService())->roomAddress($community_id,$group,$building,$unit,$room);
    	var_dump($roominfo);
    }
    //小程序积分商城绑定房屋
    public function shopAddroom(){

		$group=request()->post("group");
		$building=request()->post("building");
		$unit=request()->post("unit");
		$room=request()->post("room");
		$uid=request()->post("uid");
		//Log::write(request()->post(),"Yang");
		//根据小区和完整门牌号查询房屋信息
		$roominfo = (new appletService())->roomAddress(request()->post('community_id'),$group,$building,$unit,$room);
		
		if(empty($roominfo)){
			return '3';
		}
        //插入数据到数据库
        $result = (new appletService())->updateUserRoom($roominfo,$uid);
		if($result){
			return '1';
		}else{
			return '2';
		}
    }
    //小程序早早绑定房屋
    public function mcAddroom(){

		$group=request()->post("group");
		$building=request()->post("building");
		$unit=request()->post("unit");
		$room=request()->post("room");
		$uid=request()->post("uid");
		
		//根据小区和完整门牌号查询房屋信息
		$roominfo = (new appletService())->roomAddress(request()->post('community_id'),$group,$building,$unit,$room);
		
		if(empty($roominfo)){
			return '3';
		}
        //插入数据到数据库
        $result = (new appletService())->mcUpdateUserRoom($roominfo,$uid);
		if($result){
			return '1';
		}else{
			return '2';
		}
    }
	//提交信息计算物业费
    public function details()
    {
		$group=request()->get("group");
		$building=request()->get("building");
		$unit=request()->get("unit");
		$room=request()->get("room");

		//根据小区和完整门牌号查询房屋信息
		$roomlist = (new appletService())->roomAddress(request()->get('adcode'),$group,$building,$unit,$room);
		//echo "<pre>";
		//var_dump($roomlist);
		//根据房号查询账单
		$billlis = (new appletService())->billRoomIdList($roomlist['out_room_id']);
		//var_dump($billlis);
		if(empty($billlis)){
			return json(['error' => '暂无可缴费账单']);
		}
		$billlist = (new appletService())->cost_type($roomlist['out_room_id']);
		
		foreach ($billlist as $b){
			$lis[] = (new appletService())->select($roomlist['out_room_id'],$b);
		}
		/*var_dump($lis);
		
		foreach($billlis as $list){
			$info['acct_period'][] = $list['acct_period'];
			$info['cost_type'][] = $list['cost_type'];
			$info['bill_amount'][] = $list['bill_amount'];
			$info['id'][] = $list['id'];
		}*/
		
		return json_encode($lis);

    }

   	
	
}